package com.briup.cloud.framework.resource.config;

import com.briup.framework.utils.common.StringUtil;
import com.briup.framework.utils.execption.BriupFrameworkException;
import com.briup.framework.utils.web.Response;
import com.briup.framework.utils.web.ResponseCode;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import feign.FeignException;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.oauth2.common.exceptions.OAuth2Exception;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

/**
 * @Author guomiao
 * @Date 2022/3/22 14:20
 */

@RestControllerAdvice
public class GloabalExceptionHandler {

    @Autowired
    ObjectMapper mapper;

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class ErrorMsg{
        private String error;
        private String error_description;
    }
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Response handlerMethodArgumentNotValidException(MethodArgumentNotValidException ex){
        String message = null;
        FieldError fieldError = ex.getBindingResult().getFieldError();
        if( fieldError != null){
            message = fieldError.getDefaultMessage();
        }
        return Response.error(ResponseCode.DATA_VALIDATE_ERROR,message);
    }

    @ExceptionHandler(FeignException.class)
    public Response<String> handlerFeignException(FeignException ex){
        System.out.println("ex...."+ex.contentUTF8());
        System.out.println(ex);
        ErrorMsg map = new ErrorMsg();
        try {
            if(!StringUtil.isNullOrEmpty(ex.contentUTF8())) {
                map = mapper.readValue(ex.contentUTF8(), ErrorMsg.class);
                System.out.println("------------>转换后的" + map);
            }
        } catch (JsonProcessingException e) {
            System.out.println(" 为啥转换出错！"+e);
        }
        return Response.error(ex.status(),map.getError(),map.getError_description());
    }

    @ExceptionHandler(BriupFrameworkException.class)
    public Response<String> handlerBriupFrameworkException(BriupFrameworkException exception){
//        System.out.println("全局BriupFrameworkException.异常处理........................");
        exception.getMessage();
        return Response.error(exception.getCode(),exception.getMessage());
    }

    @ExceptionHandler(AccessDeniedException.class)
    public Response<String> handlerAccessDenied(AccessDeniedException ex){
        return Response.error(ResponseCode.USER_ACCESS_DENY,ex.getMessage());
    }

    @ExceptionHandler(DataAccessException.class)
    public Response<String> handlerException(DataAccessException e){
        System.out.println("数据库访问异常！");
        e.printStackTrace();
        return Response.error(ResponseCode.ERROR,"数据访问异常，请联系管理员！");
    }

    @ExceptionHandler(Exception.class)
    public Response<String> handlerException(Exception e){
//        System.out.println("============>"+e);
        e.printStackTrace();
        return Response.error(ResponseCode.ERROR,e.getMessage());
    }
}
